package com.skit.main.activity

import android.util.Log
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.skit.common.base.mvvm.BaseActivity
import com.skit.common.bean.ActivityBean
import com.skit.common.config.RouteConfig
import com.skit.common.event.EventMessage
import com.skit.common.ktx.NetResultKtx.initData
import com.skit.common.manager.UserManager
import com.skit.common.utils.ToastUtils
import com.skit.main.R
import com.skit.main.adapter.ActivityAdapter
import com.skit.main.databinding.MainActivityUserCollectListBinding
import com.skit.main.viewmodels.ActivityViewModel
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@AndroidEntryPoint
@Route(path = RouteConfig.MainRoute.MAIN_USER_COLLECT_ACTIVITY)
class UserCollectActivityListActivity : BaseActivity<MainActivityUserCollectListBinding>() {

    companion object {
        private const val TAG = "UserCollectActivityList"
    }

    private lateinit var activityAdapter: ActivityAdapter

    override val layoutResId: Int
        get() = R.layout.main_activity_user_collect_list

    val viewModel: ActivityViewModel by viewModels()

    val list: MutableList<ActivityBean> = ArrayList()

    override fun initData() {
        val user = UserManager.user
        if (user != null) {
            viewModel.getUserCollectActivityList(false, user.id)
        }
    }

    override fun initConfig() {
        viewModel.activityLiveData.observe(this) {
            it.initData({
                ToastUtils.showToast(msg)
            }) {
                list.addAll(data)
                activityAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        binding.recyclerView.apply {
            adapter = ActivityAdapter(list).also {
                activityAdapter = it
                it.onItemClickListener = { view, i ->
                    ARouter.getInstance().build(RouteConfig.MainRoute.MAIN_ACTIVITY_DETAILS)
                        .withSerializable("bean", list[i])
                        .navigation()
                    Unit
                }
            }
            layoutManager = LinearLayoutManager(this@UserCollectActivityListActivity)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    override fun event(event: EventMessage) {
        if (event.code == EventMessage.MessageCode.CANCEl_ACTIVITY_COLLECT) {
            event.obj?.also {
                val i = it as ActivityBean
                list.forEachIndexed { index, activityBean ->
                    Log.d(TAG, "event: ${index}")
                    if (activityBean.id == i.id) {
                        Log.d(TAG, "event activityBean: ${index}")
                        list.removeAt(index)
                        activityAdapter.notifyItemRemoved(index)
                        return@forEachIndexed
                    }
                }
            }
        } else if (event.code == EventMessage.MessageCode.ADD_ACTIVITY_COLLECT) {
            event.obj?.also {
                if (it is ActivityBean) {
                    val i = it
                    list.add(i)
                    activityAdapter.notifyItemInserted(list.size)
                }
            }
        }
        super.event(event)
    }

}